// import {rootInjector} from "./Injector";
import "reflect-metadata"
import {Injector, rootInjector} from "./Injector";

// 注解属性存储
export function InjectTable(_constructor: any): any {
    // console.log("得到了注册：",_constructor)
    rootInjector.setProvider(_constructor, _constructor)
    return _constructor
}

export function Inject(_constructor: any, propertyName: string): any {
    // 元数据反射 获取当前装饰的元素的类型
    const propertyType: any = Reflect.getMetadata('design:type', _constructor, propertyName)
    const injector: Injector = rootInjector

    let providerInsntance = injector.getInstance(propertyType)
    // 维护一个单列
    if (!providerInsntance) {
        const providerClass = injector.getProvider(propertyType)
        providerInsntance = new providerClass();
        injector.setInstance(propertyType, providerInsntance)
    }
    _constructor[propertyName] = providerInsntance
    return (_constructor as any)[propertyName];
}